package com.personnel.controller;

import com.personnel.common.StateEnum;
import com.personnel.mapper.DepartmentMapper;
import com.personnel.mapper.JobMapper;
import com.personnel.mapper.StaffMapper;
import com.personnel.pojo.Department;
import com.personnel.pojo.Job;
import com.personnel.pojo.Staff;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: YuePeng
 * @Date: 2021/11/25 17:37
 * @Version: 1.0
 */

@Controller
@RequestMapping("test")
public class TestController {

    @Resource
    private StaffMapper staffMapper;
    @Resource
    private JobMapper jobMapper;
    @Resource
    private DepartmentMapper departmentMapper;

    @GetMapping("hello")
    @ResponseBody
    public String test() {
        System.out.println("Hello!");
        System.out.println("更新1!");
        System.out.println("更新2!");
        System.out.println("更新3!");
        System.out.println("select count(staff_id) as value ,department_name as name\n" +
                "from staff\n" +
                "    left join department on department_id = staff_department_id\n" +
                "group by staff_department_id");
        return "Hello SpringBoot!!";
    }

    @GetMapping("selectForDepartment")
    @ResponseBody
    public Map<String, Object> test02() {
        List<Staff> staff = this.staffMapper.selectForDepartment();
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", staff);
        System.out.println("======" + map);
        return map;
    }

    @GetMapping("tree")
    @ResponseBody
    public List test03() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> depMap = new HashMap<>();
        Map<String, Object> jobMap = new HashMap<>();
        depMap.put("title", "人事部");
        jobMap.put("title", "人事专员");
        jobMap.put("jobId", 8);
        depMap.put("children", jobMap);
        list.add(depMap);
        System.out.println(list);
        return list;

    }

    @GetMapping("tree2")
    @ResponseBody
    public Map<String, Object> test04() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> jobMap = new HashMap<>();

        List<Job> jobsList = this.jobMapper.selectAll();
        for (Job job : jobsList) {
            Map<String, Object> depMap = new HashMap<>();
            depMap.put("title", job.getDepartment().getDepartmentName());
            depMap.put("id", job.getJobDepartmentId());
            list.add(depMap);
        }
        //return jobsList;
        HashMap<String, Object> returnMap = new HashMap<>();
        returnMap.put("data", list);
        return returnMap;
    }

    @GetMapping("tree3")
    @ResponseBody
    public List<Map<String, Object>> test05() {
        List<Map<String, Object>> list = new ArrayList<>();
        List<Map<String, Object>> eachJobList = new ArrayList<>();
        List<Department> departments = this.departmentMapper.selectAll();
        for (Department department : departments) {
            Map<String, Object> depMap = new HashMap<>();
            depMap.put("id",department.getDepartmentId());
            depMap.put("jbId",department.getDepartmentId());
            depMap.put("title",department.getDepartmentName());
            List<Job> jobList = this.jobMapper.selectByDepartmentId(department.getDepartmentId() + "");
            for (Job job : jobList) {
                Map<String, Object> jobMap = new HashMap<>();
                jobMap.put("id",job.getJobId());
                jobMap.put("title",job.getJobName());
                jobMap.put("jdId",job.getJobDepartmentId());
                jobMap.put("jdName",job.getDepartment().getDepartmentName());
                eachJobList.add(jobMap);
            }
            depMap.put("children",eachJobList);
            depMap.put("spread",true);
            eachJobList = new ArrayList<>(); //清空一次，不然每个depMap都有全部职位
            list.add(depMap);
        }
        return list;
    }

    @GetMapping("/zz")
    @ResponseBody
    public void test01(@RequestParam("state") StateEnum state){
        System.out.println(state.value());
    }
}
